package io.renren.modules.work.controller;

import java.io.IOException;
import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.work.entity.*;
import io.renren.modules.work.service.ScGoodsService;
import io.renren.modules.work.service.ScPurdtlService;
import io.renren.vo.ScConsumablesVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.work.service.ScPurService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 采购单
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-05-08 23:01:13
 */
@RestController
@RequestMapping("work/scpur")
public class ScPurController extends AbstractController {
    @Autowired
    private ScPurService scPurService;

    @Autowired
    private ScPurdtlService scPurdtlService;


    @Autowired
    private ScGoodsService scGoodsService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("work:scpur:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = scPurService.queryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("work:scpur:save")
    public R save(@RequestBody ScPurEntity scPur){
		scPurService.save(scPur);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody String params){
        return scPurService.updatePur(params);

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @Transactional
    public R delete(@RequestBody String[] purids){
		scPurService.removeByIds(Arrays.asList(purids));
        //删除细单
        for (String purid : purids) {
            QueryWrapper<ScPurdtlEntity> scPurdtlEntityQueryWrapper = new QueryWrapper<>();
            scPurdtlEntityQueryWrapper.eq("purid",purid);
            scPurdtlService.remove(scPurdtlEntityQueryWrapper);
        }
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{purid}")
    public R info(@PathVariable("purid") String purid){
        QueryWrapper<ScPurEntity> scPurEntityQueryWrapper = new QueryWrapper<>();
        scPurEntityQueryWrapper.eq("purid",purid);
        ScPurEntity scPurEntity = scPurService.getOne(scPurEntityQueryWrapper);
        QueryWrapper<ScPurdtlEntity> scPurdtlEntityQueryWrapper= new QueryWrapper<>();
        scPurdtlEntityQueryWrapper.eq("purid",purid);
        List<ScPurdtlEntity> scPurdtlEntityList = scPurdtlService.list(scPurdtlEntityQueryWrapper);
        ArrayList<ScPurdtlEntity> scPurdtlEntityArrayList = new ArrayList<>();
        for (ScPurdtlEntity scPurdtlEntity : scPurdtlEntityList) {
            ScPurdtlEntity scPurdtl = new ScPurdtlEntity();
            BeanUtil.copyProperties(scPurdtlEntity,scPurdtl);
            ScGoodsEntity goods = scGoodsService.getById(scPurdtl.getGoodsid());
            scPurdtl.setGoodsname(goods.getGoodsname());
            scPurdtlEntityArrayList.add(scPurdtl);
        }
        //成功返回结果,返回json对象
        Map<String, Object> result = new HashMap<>();
        result.put("purid",scPurEntity.getPurid());
        result.put("purno",scPurEntity.getPurno());
        result.put("purname",scPurEntity.getPurname());
        result.put("purtype",scPurEntity.getPurtype());
        result.put("memo",scPurEntity.getMemo());
        result.put("scpurdtl",scPurdtlEntityArrayList);
        return R.ok().put("data", JSON.toJSONString(result));
    }



    /**
     * 提交保存采购表单
     */
    @RequestMapping("/commit")
    public R commitProject(@RequestBody String params){
        SysUserEntity user = getUser();

        return scPurService.commitProject(params,user);
    }

    /**
     * 确认收货
     */
    @RequestMapping("/confirmRg")
    @Transactional
    public R confirmRg(@RequestBody String[] purids){
        return scPurService.confirmRg(purids);
    }

    /**
     * 绑定采购单
     */
    @RequestMapping("bindPur")
    public R bindPur(@RequestParam String purid){
        return scPurService.bindPur(purid);
    }

    /**
     * 导入采购单excel
     */
    @PostMapping("/importPur")
    public R importPur(MultipartFile file) throws IOException {
        //获取文件名
        String fileName = file.getOriginalFilename();
        //获取文件类型
        String fileType = "";
        if (fileName != null) {
            fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        if ("xlsx".equals(fileType) || "xls".equals(fileType)) {
            return scPurService.importExcel(file);
        }else{
            return R.error("非Excel类型");
        }

    }

}
